package com.youku.adSDK.view
{
   import flash.display.MovieClip;
   import com.youku.adSDK.ADEventDispather;
   import com.youku.adSDK.view.components.VideoAD;
   import com.youku.adSDK.model.vo.GroupData;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.ADConstant;
   import com.youku.adSDK.view.events.VideoADEvent;
   import com.youku.adSDK.YoukuAdEvent;
   
   public class BackMediator extends Object
   {
      
      public function BackMediator(param1:ADEventDispather, param2:ADContext) {
         super();
         this._mainMc = param2.mainMc;
         this._context = param2;
         this._eventDispather = param1;
      }
      
      private var _mainMc:MovieClip;
      
      private var _eventDispather:ADEventDispather;
      
      private var _back:VideoAD;
      
      private var _backData:GroupData;
      
      private var _width:Number = 0;
      
      private var _height:Number = 0;
      
      private var _barH:Number;
      
      private var _shutData:Boolean = false;
      
      private var _volume:Number = -1;
      
      private var _loaded:Boolean = false;
      
      private var _mustLoad:Boolean = false;
      
      private var _context:ADContext;
      
      public function init() : void {
         this._eventDispather.addEventListener(ADEvent.VIDEO_PLAYING,this.onPlayerPlaying);
         this._eventDispather.addEventListener(ADEvent.REQUEST_AND_PLAY_BACK_AD,this.onPlayerShowBackAD);
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
         this._eventDispather.addEventListener(ADEvent.COMMON_ERROR,this.onPlayerError);
         this._eventDispather.addEventListener(ADEvent.COMMON_RESET,this.onPlayerReset);
         this._eventDispather.addEventListener(ADEvent.CHANGE_VOLUME,this.onPlayerChangeVolume);
         this._eventDispather.addEventListener(ADEvent.COMMON_START_TO_PLAY,this.onPlayerReplay);
         this._eventDispather.addEventListener(ADEvent.BACK_DATA_LOADED,this.onDataProxyEvent);
         this._eventDispather.addEventListener(ADEvent.BACK_DATA_LOAD_ERROR,this.onDataProxyEvent);
      }
      
      private function onDataProxyEvent(param1:ADEvent) : void {
         switch(param1.type)
         {
            case ADEvent.BACK_DATA_LOADED:
               this.initBack(param1.data as GroupData);
               break;
            case ADEvent.BACK_DATA_LOAD_ERROR:
               this.backDone();
               break;
         }
      }
      
      private function onPlayerReplay(param1:ADEvent) : void {
         this._shutData = false;
         this._loaded = false;
         this._mustLoad = false;
      }
      
      private function onPlayerPlaying(param1:ADEvent) : void {
         if(!param1.data.nsTime)
         {
            return;
         }
         var _loc2_:Number = param1.data.nsTime;
         var _loc3_:Number = this._context.totalTime;
         if(!this._loaded && _loc3_ - _loc2_ <= 10)
         {
            this.loadBackData();
            this._loaded = true;
         }
      }
      
      private function loadBackData() : void {
         this._shutData = false;
         this._eventDispather.dispatchEvent(new ADEvent(ADEvent.DO_BACK));
      }
      
      private function onPlayerShowBackAD(param1:ADEvent) : void {
         if(this._context.isTudouPlayer)
         {
            this.onBackPlayOver(null);
            return;
         }
         if(this._back)
         {
            this._back.visible = true;
            this._back.play();
         }
         else
         {
            this._mustLoad = true;
            if(!this._loaded)
            {
               this.loadBackData();
               this._loaded = true;
            }
            else
            {
               this.backDone();
            }
         }
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._width = param1.data.width;
         this._height = param1.data.height;
         this._barH = param1.data.cheight;
      }
      
      private function onPlayerReset(param1:ADEvent) : void {
         this.close();
      }
      
      private function onPlayerError(param1:ADEvent) : void {
         this.close();
      }
      
      private function close() : void {
         this._shutData = true;
         this._loaded = false;
         this._mustLoad = false;
         this.disposeBack();
      }
      
      private function onPlayerChangeVolume(param1:ADEvent) : void {
         this._volume = param1.data.volume;
      }
      
      private function initBack(param1:GroupData) : void {
         this.disposeBack();
         this._back = new VideoAD();
         this._back.ADType = ADConstant.BACK_AD;
         this._back.setVolume(this._volume);
         this._mainMc.addChild(this._back);
         this._back.addEventListener(VideoADEvent.LOAD_COMPLETE,this.onBackLoadComplete);
         this._back.addEventListener(VideoADEvent.PLAY_OVER,this.onBackPlayOver);
         this._back.addEventListener(VideoADEvent.PLAY_BLOCKED,this.onBackPlayOver);
         this._back.addEventListener(VideoADEvent.PLAY_ERROR,this.onBackPlayOver);
         if(!this._width)
         {
            this._width = this._mainMc.stage.stageWidth;
            this._height = this._mainMc.stage.stageHeight;
            this._barH = 40;
         }
         this._back.init(param1.VAL,this._context,this._eventDispather,this._width,this._height,this._barH);
         this._back.visible = false;
         if(this._mustLoad)
         {
            this._back.visible = true;
            this._back.play();
         }
      }
      
      private function onBackLoadComplete(param1:VideoADEvent) : void {
      }
      
      private function onBackPlayOver(param1:VideoADEvent) : void {
         this.disposeBack();
         this.backDone();
      }
      
      private function disposeBack() : void {
         if(this._back)
         {
            this._back.dispose();
            this._back = null;
         }
      }
      
      private function backDone() : void {
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_VIDEO_PLAY_COMPLETE));
      }
   }
}
